/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-01     chengjili       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#if defined(BSP_USING_USER_DAC1) || defined(BSP_USING_USER_DAC2)

#define DRV_DEBUG
#define LOG_TAG             "user.dac"
#include <drv_log.h>


struct n32_dac
{
    struct rt_dac_device n32_dac_device;
    DAC_Module *DAC_Handler;
    char *name;
};

static struct n32_dac n32_dac_obj =
{
    .DAC_Handler = DAC,
    .name = "dac"
};


static rt_uint32_t n32_dac_get_channel(rt_uint32_t channel)
{
    rt_uint32_t n32_channel = 0;

    switch (channel)
    {
        case  1:
            n32_channel = DAC_CHANNEL_1;
            break;
        case  2:
            n32_channel = DAC_CHANNEL_2;
            break;
    }

    return n32_channel;
}

static rt_err_t n32_dac_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{
    DAC_Module *n32_dac_handler;
    DAC_InitType DAC_InitStructure;
    RT_ASSERT(device != RT_NULL);
    n32_dac_handler = device->parent.user_data;

    rt_uint32_t ch = n32_dac_get_channel(channel);

    GPIO_InitType GPIO_InitCtlStruct;
    GPIO_InitStruct(&GPIO_InitCtlStruct);

    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);

    if(ch == DAC_CHANNEL_1)
    {
        GPIO_InitCtlStruct.Pin = GPIO_PIN_4;
        GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
    }
    if(ch == DAC_CHANNEL_2)
    {
        GPIO_InitCtlStruct.Pin = GPIO_PIN_5;
        GPIO_InitCtlStruct.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitCtlStruct.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_InitPeripheral(GPIOA, &GPIO_InitCtlStruct);
    }

    DAC_InitStructure.BufferOutput = DAC_BUFFOUTPUT_ENABLE;
    DAC_InitStructure.LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
    DAC_InitStructure.Trigger = DAC_TRG_NONE;
    DAC_InitStructure.WaveGen = DAC_WAVEGEN_NONE;
    DAC_Init(ch, &DAC_InitStructure);

    DAC_Enable(ch, ENABLE);

    return RT_EOK;
}

static rt_err_t n32_dac_disabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{
    DAC_Module *n32_dac_handler;
    RT_ASSERT(device != RT_NULL);
    n32_dac_handler = device->parent.user_data;

    rt_uint32_t ch = n32_dac_get_channel(channel);

    DAC_Enable(ch, DISABLE);

    return RT_EOK;
}

static rt_err_t n32_set_dac_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
    DAC_Module *n32_dac_handler;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(value != RT_NULL);

    n32_dac_handler = device->parent.user_data;

    rt_uint32_t ch = n32_dac_get_channel(channel);

    if(ch == DAC_CHANNEL_1)
        DAC_SetCh1Data(DAC_ALIGN_R_12BIT, *value);
    if(ch == DAC_CHANNEL_2)
            DAC_SetCh2Data(DAC_ALIGN_R_12BIT, *value);

    return RT_EOK;
}

static const struct rt_dac_ops at_dac_ops =
{
    .disabled = n32_dac_disabled,
    .enabled = n32_dac_enabled,
    .convert = n32_set_dac_value,
};

static int rt_hw_dac_init(void)
{
    int result = RT_EOK;
    /* register dac device */
    if (rt_hw_dac_register(&n32_dac_obj.n32_dac_device,
                n32_dac_obj.name, &at_dac_ops,
                n32_dac_obj.DAC_Handler) == RT_EOK)
    {
        LOG_D("%s register success", n32_dac_obj.name);
    }
    else
    {
        LOG_E("%s register failed", n32_dac_obj.name);
        result = -RT_ERROR;
    }

    return result;
}
INIT_BOARD_EXPORT(rt_hw_dac_init);






#endif

